#include <GQE/Core.hpp>
#include <iostream>
#include "States/InventoryState.hpp"
#include "States/GetItemState.hpp"
#include "Objects/ButtonObject.hpp"
#include "classes/Command.hpp"
#include "Game_types.hpp"
#include "classes/PlayerClass.hpp"
#include <time.h>
//\#define DEBUG_DRAW
ButtonObject::ButtonObject(GQE::App* theApp, GQE::typeObjectID theID, unsigned char theFlags):
    IObject(theApp, theID, theFlags),
    mPressed(false),
	mLong(false),
	mMaxChars(0),
	mIndex(0)
{

}

ButtonObject::~ButtonObject()
{
}
/**
* DoInit is responsible for initializing this Object
*/
void ButtonObject::DoInit()
{
    IObject::DoInit();
    //Font
    mTopString.SetFont(mApp->mAssetManager.GetFont("MainFont")->GetAsset());
    mMidString.SetFont(mApp->mAssetManager.GetFont("MainFont")->GetAsset());
    mBottomString.SetFont(mApp->mAssetManager.GetFont("MainFont")->GetAsset());
    //Text
    mTopString.SetText("__________");
    mBottomString.SetText("----------");
    //Color
    mTopString.SetColor(sf::Color::Black);
    mMidString.SetColor(sf::Color::Black);
    mBottomString.SetColor(sf::Color::Black);

    mTopString.SetSize(16);
    mMidString.SetSize(16);
    mBottomString.SetSize(16);
}
void ButtonObject::HandleEvents(sf::Event theEvent)
{

}
void ButtonObject::Collision(IObject* other, GQE::typeCollisionResult result)
{

}

void ButtonObject::UpdateFixed(void)
{
    const sf::Input& Input=mApp->mWindow.GetInput();
    if(Input.IsMouseButtonDown(sf::Mouse::Left)&& Input.GetMouseX()>mPosition.x+mClickArea.Left &&Input.GetMouseY()>mPosition.y+mClickArea.Top&& Input.GetMouseX()<mPosition.x+mClickArea.Right && Input.GetMouseY()<mPosition.y+mClickArea.Bottom)
    {
        if(!mPressed)
        {
            if(mAction=="Move North")
            {
                Commands::Move(NORTH);
            }
            else if(mAction=="Move East")
            {
                Commands::Move(EAST);
            }
            else if(mAction=="Move South")
            {
                Commands::Move(SOUTH);
            }
            else if(mAction=="Move West")
            {
                Commands::Move(WEST);
			}
			else if(mAction=="Move Down")
            {
                Commands::Move(DOWN);
            }
			else if(mAction=="Move Up")
            {
                Commands::Move(UP);
            }
			else if(mAction=="Look")
			{
				Commands::Look();
			}
			else if(mAction=="Exit")
			{
			    mApp->mStateManager.RemoveActiveState();
			}
			else if(mAction=="Inventory")
			{
			    mApp->mStateManager.AddActiveState(new InventoryState(mApp));
			}
			else if(mAction=="Item")
			{
			    typeInventory* anInventory=gPlayer->GetInventory();
                if(mIndex>=0 && mIndex<anInventory->size())
                {
                    gSelectItem=&anInventory->at(mIndex);
                    gMainWindow->Clear();
                    gMainWindow->NewLine(gSelectItem->Item.Name + "X" +JDD::IntToString(gSelectItem->Amount));
                    gMainWindow->NewLine(gSelectItem->Item.Info);
                    gMainWindow->NewLine("Power: "+gSelectItem->Item.Power+", Grade: "+gSelectItem->Item.Grade+", Type: "+gSelectItem->Item.Type+", SubType: "+ gSelectItem->Item.SubType+", Stackable: "+gSelectItem->Item.Stackable+", TwoHanded: "+gSelectItem->Item.TwoHanded);
                }
			}
			else if(mAction=="Get")
			{
                mApp->mStateManager.AddActiveState(new GetItemState(mApp));
			}
            mPressed=true;
        }
    }
    else
    {
        mPressed=false;
    }
    mMidString.SetPosition(mPosition.x,mPosition.y+16);
    mTopString.SetPosition(mPosition.x,mPosition.y);
    mBottomString.SetPosition(mPosition.x,mPosition.y+32);
    IObject::UpdateFixed();
}
void ButtonObject::UpdateVariable(float theElapsedTime)
{
    IObject::UpdateVariable(theElapsedTime);
}
void ButtonObject::Draw()
{
#ifdef DEBUG_DRAW
    sf::Shape anShape=sf::Shape::Rectangle(mClickArea.Left+mPosition.x,mClickArea.Top+mPosition.y,mClickArea.Right+mPosition.x,mClickArea.Bottom+mPosition.y,sf::Color(200,200,200));
    mApp->mWindow.Draw(anShape);
#endif
    if(!mLong)
    {
        mApp->mWindow.Draw(mTopString);
        mApp->mWindow.Draw(mBottomString);
    }

    mApp->mWindow.Draw(mMidString);

    IObject::Draw();
}
void ButtonObject::Cleanup()
{
    IObject::Cleanup();
}
void ButtonObject::SetText(std::string theText)
{
    if(mLong)
	{
		mMaxChars=20;
		mClickArea=sf::IntRect(0,16,gConfig->GetUint32("ListInfo","Width",192),gConfig->GetUint32("ListInfo","Height",16)+16);
	}
	else
	{
		mMaxChars=9;
		mClickArea=sf::IntRect(0,16,gConfig->GetUint32("ButtonInfo","Width",192),gConfig->GetUint32("ButtonInfo","Height",16)+16);
	}
    std::string anFormText;
    anFormText="|";
    anFormText+=theText;
    if(anFormText.length()>mMaxChars)
    {
        anFormText=anFormText.substr(0,mMaxChars);
    }
    while(anFormText.length()<mMaxChars)
    {
        anFormText+=" ";
    }
    anFormText+="|";
    mMidString.SetText(anFormText);
    mText=theText;
}
void ButtonObject::SetLong(bool theLong)
{
	mLong=theLong;
}
